package per.chenxin.util.tcp.http;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class ProxyCheck {
	static String CONFIG = "config.ini";

	static String strCheckUrl = "http://www.google.com";

	static String strIgs = "igs.joyjoy.net:7777";

	// static String strCheckUrl = "http://www.baidu.com";

	public static void main(String args[]) throws FileNotFoundException,
			IOException {
		ProxyEntity proxyEntity = ProxyEntity
				.readFromIni(args.length > 1 ? args[0] : CONFIG);

		LineNumberReader input = null;
		try {
			input = new LineNumberReader(new InputStreamReader(
					new FileInputStream(args[args.length - 1])));
			String strLine = input.readLine();
			while (strLine != null) {
				if (strLine.trim().length() == 0)
					continue;
				// new ProxyCheckThread(proxyEntity, strLine).run();
				(new Thread(new ProxyCheckThread(proxyEntity, strLine)))
						.start();
				strLine = input.readLine();
			}

		} finally {
			if (input != null)
				input.close();
		}

	}

	static class ProxyCheckThread implements Runnable {

		ProxyEntity proxyEntity;
		String strTest;

		public ProxyCheckThread(ProxyEntity proxyEntity, String strTest) {
			this.proxyEntity = proxyEntity;
			this.strTest = strTest;
		}

		public void run() {

			Socket socket = null;
			try {
				long longStart = System.currentTimeMillis();
				socket = HttpUtil.getSocketFromProxy(proxyEntity);
				if (socket == null) {
					socket = new Socket(strTest.split(":")[0],
							Integer.parseInt(strTest.split(":")[1]));
				} else {
					try {
						HttpUtil.socketConnect(socket, strTest.split(":")[0],
								Integer.parseInt(strTest.split(":")[1]));
						HttpUtil.socketConnect(socket, strTest.split(":")[0],
								Integer.parseInt(strTest.split(":")[1]));
					} catch (IOException e) {
						return;
					}
				}

				socket.setSoTimeout(HttpUtil.INT_SO_TIMEOUT);
				byte[] buffer = null;

				if ("connect".equalsIgnoreCase(proxyEntity.getCheckMethod())) {
					HttpUtil.socketConnect(socket, proxyEntity.getCheckUrl()
							.split(":")[0], Integer.parseInt(proxyEntity
							.getCheckUrl().split(":")[1]));
					buffer = HttpUtil.getBytesFromInputStream(socket
							.getInputStream());
				} else {
					buffer = HttpUtil.getMethod(socket,
							proxyEntity.getCheckUrl(), true,
							HttpUtil.getLastAuthorization(proxyEntity));
				}
				if (buffer == null)
					return;

				BufferedReader in = new BufferedReader(new StringReader(
						new String(buffer, "ISO-8859-1")));
				String line = in.readLine();
				if (line == null || !line.matches(proxyEntity.getCheckResult())) {
					return;
				}

				System.out.println(strTest + " "
						+ (System.currentTimeMillis() - longStart) + "ms");
			} catch (UnknownHostException e) {
				// e.printStackTrace();
			} catch (IOException e) {
				// e.printStackTrace();
			} finally {
				if (socket != null)
					try {
						socket.close();
					} catch (IOException e) {
					}
			}

		}

	}

}
